﻿#region

using System.Data.Objects.DataClasses;
using Core.DAO;

#endregion

namespace Core.BO
{
    /// <summary>
    ///     Abstract class for all custom BO class.
    ///     This class will help run common function of TEntity: Add, Update, Delete and others.
    /// </summary>
    /// <typepara name="TDAO">DAO class inherit from <see cref="IDAO{TEntity}" /></typepara>
    /// <typepara name="TEntity">Entity class inherit from <see cref="EntityObject" /></typepara>
    public class BaseBO<TDAO, TEntity>
        where TEntity : EntityObject
        where TDAO : IDAO<TEntity>, new()
    {
        /// <summary>
        ///     Add a object into DAO class
        /// </summary>
        /// <para name="entityObj">A new object</para>
        public void Add(TEntity entityObj)
        {
            using (var scope = new DAOScope())
            {
                var tDao = new TDAO();
                tDao.Add(entityObj);
                scope.Complete();
            }
        }

        /// <summary>
        ///     Update a object to DAO class
        /// </summary>
        /// <para name="entityObj">A object need update into DAO class</para>
        public void Update(TEntity entityObj)
        {
            using (var scope = new DAOScope())
            {
                var tDao = new TDAO();
                tDao.Update(entityObj);
                scope.Complete();
            }
        }

        /// <summary>
        ///     Delete a object from DAO class
        /// </summary>
        /// <para name="entityObj">A object need delete</para>
        public void Delete(TEntity entityObj)
        {
            using (var scope = new DAOScope())
            {
                var tDao = new TDAO();
                tDao.Delete(entityObj);
                scope.Complete();
            }
        }
    }
}